package online.westonline.com.westonline.ui.fragment.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.easy.west.westclient.Constants;
import com.easy.west.westclient.api.base.bean.SuccessBean;
import com.easy.west.westclient.api.message.bean.MessageBean;
import com.easy.west.westclient.api.message.event.FindAllMessageEvent;
import com.easy.west.westclient.api.message.event.ReadMessageEvent;
import com.orhanobut.hawk.Hawk;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import online.westonline.com.westonline.MyApplication;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.adapter.messgae_notice.MessageNoticeAdapter;
import online.westonline.com.westonline.base.BaseFragment;
import online.westonline.com.westonline.entry.PushDataBean;
import online.westonline.com.westonline.event.MessmageDownEvent;
import online.westonline.com.westonline.ui.MeetRoomStateActivity;
import online.westonline.com.westonline.utils.CacheUtils;
import online.westonline.com.westonline.utils.UiUtlis;

/**
 * 未读界面
 * Created by xiatao on 2018/8/30.
 */

public class UnReadFragment extends BaseFragment {
    @BindView(R.id.recyclerView_base)
    RecyclerView recyclerViewBase;
    @BindView(R.id.swipe_base_layout)
    SwipeRefreshLayout swipeBaseLayout;
    Unbinder unbinder;
    private List<MessageBean> messageBeanList = new ArrayList<>();
    private List<MessageBean> unread_list = new ArrayList<>();
    private MessageNoticeAdapter noticeAdapter;
    @Override
    protected boolean setEventOpen() {
        return true;
    }

    @Override
    protected void initdata() {
        noticeAdapter = new MessageNoticeAdapter();
        noticeAdapter.setClickListener(new MessageNoticeAdapter.onMessageClickListener() {
            @Override
            public void click(int pos, MessageBean messageBean) {
                MyApplication.getClient().readMessage(messageBean.getMesid());
                PushDataBean p = new PushDataBean();
                Bundle bundle = new Bundle();
                //一定不能为空这个东西,创建通知需要规范
                p.setType(messageBean.getType());
                switch (messageBean.getType())
                {
                    case Constants.NOTIFIY_TYPE_MEETING:
                        p.setMeeting(messageBean.getContent());
                        break;
                    case Constants.NOTIFIY_TYPE_COMMENT:
                        p.setComment(messageBean.getContent());
                        break;
                    case Constants.NOTIFIY_TYPE_TASK:
                        p.setTask(messageBean.getContent());
                        break;
                }
                bundle.putSerializable("data",p);
                Intent intent = new Intent(activity, MeetRoomStateActivity.class);
                intent.putExtras(bundle);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);

            }
        });
        //查询
        messageBeanList = CacheUtils.getInstance().getMessageList(user.getData().getAccess_token());
        if (messageBeanList!=null&&messageBeanList.size()>0)
        {

            parseData();
        }
        //请求所有界面,然后保存
        MyApplication.getClient().findAllMessage(user.getData().getId());
    }

    @Override
    protected void initview() {
        if (messageBeanList!=null)
        {
            //停止转圈
            swipeBaseLayout.setRefreshing(false);
        }
        else
        {
            swipeBaseLayout.setRefreshing(true);
        }
        swipeBaseLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //请求所有界面,然后保存
                MyApplication.getClient().findAllMessage(user.getData().getId());
            }
        });
        swipeBaseLayout.setColorSchemeColors(UiUtlis.getColor(R.color.BlueTheme),UiUtlis.getColor(R.color.TopicItemNodeTextColor)
                ,UiUtlis.getColor(R.color.Event_Green));
        initrecyclerview();
    }

    private void initrecyclerview() {

        recyclerViewBase.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerViewBase.setAdapter(noticeAdapter);
    }

    @Override
    protected int setlayout() {
        return R.layout.fragment_recyclerview;
    }

    /***
     * 获取所有消息列表
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getAllMessageEvent(FindAllMessageEvent eva)
    {
        if (eva.isOk())
        {
            messageBeanList = eva.getBean().getData();
            //储存一次
            CacheUtils.getInstance().saveMessageList(user.getData().getAccess_token(),messageBeanList);
            //拆分数据
            //停止转圈
            swipeBaseLayout.setRefreshing(false);
            parseData();
        }
    }

    /**
     * 标记已读回执
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void readMessage(ReadMessageEvent eva)
    {
        if (eva.isOk())
        {
            SuccessBean successBean = eva.getBean();
            if (successBean.getStatus()==1)
            {
                //请求所有界面,然后保存
                MyApplication.getClient().findAllMessage(user.getData().getId());
            }
        }
    }
    /**
     * 处理数据
     */
    private void parseData() {
        unread_list.clear();
        //区别已读和未读,该界面需要未读的
        for (MessageBean messageBean:messageBeanList)
        {
            if (messageBean.getIsread()==0)
            {
                unread_list.add(messageBean);
            }
        }
        if (unread_list!=null)
        {
            noticeAdapter.setMessageBeanList(unread_list);
            noticeAdapter.notifyDataSetChanged();
        }

    }

    public void updateData() {
        //请求所有界面,然后保存
        MyApplication.getClient().findAllMessage(CacheUtils.getInstance().getUser().getData().getId());
    }
}
